#!/bin/sh

# if app not exist
if [ ! -f /bin/udpxy ]; then
    exit 0
fi

# wait to start web and run from goahead code
. /etc/scripts/web_wait.sh
web_wait

# get params
. /etc/scripts/global.sh

LOG="logger -t udpxy"

start() {
    get_param
    if [ "$UDPXYMode" != "0" ] && [ "$UDPXYPort" != "" ]; then
	# check ip adress set at wan
	wan_is_not_null=`ip -4 addr show $wan_if | grep inet -c`
	if [ "$wan_is_not_null" = "0" ]; then
    	    $LOG "Wait 10 sec before start..."
	    sleep 10
	fi
        $LOG "UDPXY start"
	udpxy -m $wan_if -n-20 -c1 -H10 -R-1 -B64k -p"$UDPXYPort" -l /var/log/udpxy.log
    fi
}

get_param() {
    eval `nvram_buf_get 2860 UDPXYPort`
}

stop() {
  $LOG "UDPXY stop"
    killall -q udpxy
    killall -q -SIGKILL udpxy
}

case "$1" in
	start)
	    start
	    ;;

	stop)
	    stop
	    ;;

	restart)
	    stop
	    start
	    ;;

	*)
	    echo $"Usage: $0 {start|stop|restart}"
	    exit 1
esac
